package com.wcards.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wcards.config.R;
import com.wcards.dto.OrderInfoDTO;
import com.wcards.entity.IncomeInfo;
import com.wcards.entity.OrderInfo;
import com.wcards.mapper.CartsMapper;
import com.wcards.mapper.IncomeInfoMapper;
import com.wcards.mapper.OrderInfoMapper;
import com.wcards.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/*
订单表
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private IncomeInfoMapper incomeInfoMapper;

    @Resource
    private CartsMapper cartsMapper;

    //生成订单
    @Transactional
    @Override
    public R<String> creatOrder(OrderInfoDTO orderInfoDTO) {
        //订单记录
        IncomeInfo incomeInfo=new IncomeInfo();
        incomeInfo.setObjectNumbers(orderInfoDTO.getGoodsNumbers());
        incomeInfo.setIncomeType(orderInfoDTO.getIncomeType());
        incomeInfo.setUserId(orderInfoDTO.getUserId());
        incomeInfo.setTransactionTime(LocalDateTime.now());
        incomeInfo.setTransactionPrice(orderInfoDTO.getTransaction());
        incomeInfo.setIncomeNumbers(orderInfoDTO.getIncomeNumbers());
        incomeInfo.setTransactionGoods(orderInfoDTO.getTransactionGoods());

        incomeInfoMapper.insert(incomeInfo);
        orderInfoMapper.insert(orderInfoDTO);
        cartsMapper.deleteById(orderInfoDTO.getCartId());
        return R.success("生成成功！");
    }
    //删除订单
    @Override
    public R<String> deleteOrder(Integer id) {
        orderInfoMapper.deleteById(id);
        return R.success("删除成功！");
    }
    /*
     * 修改订单
     * */

    @Override
    public R<String> updateOrder(OrderInfoDTO orderInfoDTO) {

        orderInfoMapper.updateById(orderInfoDTO);
        return R.success("修改成功！");
    }

    /*
     * 查询订单
     * */

    @Override
    public R<Page<OrderInfo>> findOrder(OrderInfoDTO orderInfoDTO, Integer pageSize, Integer pageNumber) {
//根据用户ID、订单状态模糊查询
        Page<OrderInfo> orderPage=new Page<>(pageSize,pageNumber);
        LambdaQueryWrapper<OrderInfo> lqw=new LambdaQueryWrapper();
        lqw.eq(OrderInfo::getUserId,orderInfoDTO.getUserId());
        orderInfoMapper.selectPage(orderPage,lqw);
        return R.success(orderPage);
    }
}
